package com.binc.testspring.controller.rabbit;

import com.binc.testspring.dto.Student;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/rabbit")
@Slf4j
public class RabbitController {

    @Autowired
    AmqpTemplate amqpTemplate;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @PostMapping("/send")
    @ResponseBody
    @ApiOperation(value = "发送一般消息",nickname = "rabbit_send")
    public void send(String message) {
        log.info("发送消息,send 开始; 【"+message+"】");
        amqpTemplate.convertAndSend("queue1",message);
    }

    @PostMapping("/sendObject")
    @ResponseBody
    @ApiOperation(value = "发送/接收对象",nickname = "rabbit_sendObject")
    public void sendObject(@RequestBody  Student stu) {
        log.info("发送消息,sendObject 开始; 【"+stu+"】");
        amqpTemplate.convertAndSend("objectQueue1",stu); // 注意发送的对象要实现序列化
    }

    @PostMapping("/sendTopic")
    @ResponseBody
    @ApiOperation(value = "发送topic对象",nickname = "rabbit_sendTopic")
    public void sendTopic(String topicName,String message) {
        log.info("发送消息,sendTopic 开始; 【主题："+topicName+",消息："+message+"】");
        if (Objects.equals("topic.a", topicName)) {
            System.out.println("a");
            rabbitTemplate.convertAndSend("topicExchange1","topic.a1",message); //
        } else if (Objects.equals("topic.b", topicName)) {
            System.out.println("b");
            rabbitTemplate.convertAndSend("topicExchange1", "topic.boss", message); //
        } else if (Objects.equals("topic.d", topicName)) {
            System.out.println("d");
            rabbitTemplate.convertAndSend("topicExchange1", "topic.d4", message); //
        } else {
            System.out.println("c");
            rabbitTemplate.convertAndSend("topicExchange1", "topic.c", message); //
        }
    }

}
